$(function() {
    var nombre = $( "#Nombre" ),
        password = $( "#Password" ),
        allFields = $( [] ).add( nombre ).add( password ),
	tips = $( ".validateTips" );
        
        function checkRegexp( o, regexp, n ) {
			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass( "ui-state-error" );
				updateTips( n );
				return false;
			} else {
				return true;
			}
		}
                
        function checkLength( o, n, min, max ) {
			if ( o.val().length > max || o.val().length < min ) {
				o.addClass( "ui-state-error" );
				updateTips( "El " + n + " debe tener entre " +
					min + " y " + max + " caracteres." );
				return false;
			} else {
				return true;
			}
		}


        function updateTips( t ) {
			tips
				.text( t )
				.addClass( "ui-state-highlight" );
			setTimeout(function() {
				tips.removeClass( "ui-state-highlight", 1500 );
			}, 500 );
		}
$("#DialogLogin").css("visibility","visible");      
    $( "#DialogLogin" ).dialog({
			autoOpen: false,
			height: 520,
			width: 400,
			modal: true,
                   	buttons: {
				"Entrar": function() {
					var bValid = true;
					allFields.removeClass( "ui-state-error" );

					bValid = bValid && checkLength( nombre, "Nombre", 3, 45 );                                        
					//bValid = bValid && checkRegexp( nombre, /^[a-z]([0-9a-z_])+$/i, "El nombre solo puede tener a-z, 0-9, _ , debe comenzar con una letra." );
					
                                       // bValid = bValid && checkRegexp( nombre, /^([0-9a-zA-Z])+$/, "El nombre solo puede contener letras y números." );
					bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "La clave solo puede contener letras y números." );
					if ( bValid ) {
//                                                alert(calcMD5(password.val())+" \n"+nombre.val());
						$.post("../Logica/valida_usuario.php",{clave:calcMD5(password.val()),nombre:nombre.val()},function(data){
                                                   switch (data){
                                                       case 'Coord. Servicio Comunitario':
                                                          if(navigator.userAgent.indexOf('MSIE') != -1){
                                                            window.location="../InterfazIE/Administrador.php";
                                                          }else{
                                                            window.location="../Interfaz/Administrador.php";
                                                          } 
                                                           
                                                       break;
                                                       
                                                       case 'Usuario':
                                                          if(navigator.userAgent.indexOf('MSIE') != -1){
                                                            window.location="../InterfazIE/Registro.php";
                                                          }else{
                                                            window.location="../Interfaz/Registro.php";
                                                          } 
                                                           
                                                       break;
                                                       
                                                       default:
                                                           alert("Nombre de Usuario o Contraseña incorrectos");
                                                       break;
                                                   }
   
                                                });
          
						$( this ).dialog( "close" );
					}
				},
				Cancelar: function() {
					$( this ).dialog( "close" );
				}
			},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
                                updateTips( "Debe llenar ambos campos." );
			}
		});
              if(navigator.userAgent.indexOf('MSIE') == -1){
                $( "#DialogLogin" ).dialog({ height: 350 });
              } 


});

function checkNames(nombre){
    var patt=new RegExp("^([0-9a-zA-Z])+$");
    return patt.test(nombre);
}

function checkNumbers(number){
    var patt=new RegExp("^([0-9])+$");
    return patt.test(number);
}

function checkCorreo(mail){
    var patt = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i;
    return patt.test(mail);
}

function checkTexto(text){
    var patt = /^([0-9a-zA-Z,. ])+$/;
    return patt.test(text);
}

function hoverDivEnter(element,color){
    if(color == "Azul"){
      $(element).css('background-color', '#6ec8ec');   
    }else if(color == "Verde"){
      $(element).css('background-color', '#d6f94e'); 
    }else{
      $(element).css('background-color', '#fff0a5');  
    }
    
}

function hoverDivOut(element){
       $(element).css('background-color', ''); 
}

function CerrarSesion(){
    $.post("../Logica/CerrarSesion.php",{},function(data){
        if (data==1)
            window.location="Home.html";
    });
}